package com.et114.components.validCode;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

//@author guanhw
public class AuthCodeImg extends HttpServlet {
	private static final long	serialVersionUID	= 7610959641712287266L;
	public static final String	ACEGI_SECURITY_FORM_VALIDCODE_KEY	= "j_valid_code";
	private String initVaildCode ; 
	public void init ( ) throws ServletException {
		// Put your code here
		initVaildCode = getInitParameter ( ACEGI_SECURITY_FORM_VALIDCODE_KEY ) ; 
		if ( initVaildCode == null || "".equals (  initVaildCode ) )
			initVaildCode = ACEGI_SECURITY_FORM_VALIDCODE_KEY ;
		
	}
	
	protected void service ( HttpServletRequest request ,
			HttpServletResponse response ) throws ServletException ,	IOException {
		response.setContentType ( "image/jpeg" );
		ServletOutputStream out = response.getOutputStream ( );
		DrawRandNumber instance = DrawRandNumber.getInstance ( );
		BufferedImage image = ( BufferedImage ) instance.drawRandNumber ( 4 ,	request , initVaildCode );
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
		encoder.encode ( image );
		out.close ( );
	}
	
}
